package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
//        修改套餐普通信息
        setmealMapper.updateStatus(setmeal);
        ArrayList<Long> list = new ArrayList<>();
        list.add(setmeal.getId());
//       根据ID删除套餐和菜品关系
        setmealDishMapper.delete(list);
        Long id = setmeal.getId();
        if (setmealDTO.getSetmealDishes() != null && setmealDTO.getSetmealDishes().size() > 0) {
            List<SetmealDish> collect = setmealDTO.getSetmealDishes().stream().
                    map(setmealDish -> {
                        setmealDish.setSetmealId(id);
                        return setmealDish;
                    }).collect(Collectors.toList());
//        新增套餐(套餐和菜品关系)
            setmealDishMapper.save(collect);
        }
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> result = setmealDishMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(result);
        return setmealVO;
    }

    @Override
    public void delIds(List<Long> ids) {
        List<Setmeal> list = setmealMapper.getListByIds(ids);
        list.forEach(setmeal -> {
            if (setmeal.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setmealMapper.delete(ids);
        setmealDishMapper.delete(ids);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.updateStatus(setmeal);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.save(setmeal);
        Long setmealId = setmeal.getId();
        if (setmealDTO.getSetmealDishes() != null && setmealDTO.getSetmealDishes().size() > 0) {
            List<SetmealDish> result = setmealDTO.getSetmealDishes().stream().map(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
                return setmealDish;
            }).collect(Collectors.toList());
            setmealDishMapper.save(result);
        }
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //查询普通数据
        Page<Setmeal> page = setmealMapper.page(setmealPageQueryDTO);
        List<SetmealVO> collect = page.stream().map(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            //查询分页显示的套餐分类
            String sname = categoryMapper.getnameByID(setmeal.getCategoryId());
            setmealVO.setCategoryName(sname);
            return setmealVO;
        }).collect(Collectors.toList());
        return new PageResult(page.getTotal(), collect);

    }


}
